package my.game.Descension;

import java.awt.event.KeyEvent;

public class Creature {
	
	private int cursorDir; // Direction menu is moving in battle.
	private int x,y; // Coordinates
	private int dx, dy;
	private Sprite player_sprite;
	private LevelLoader level;
	private boolean alive;
	private boolean inventoryMenu = false; // Is open or not.
	private boolean selectPressed = false;
	private boolean inDangerZone = true; // Can battles be started.
	private boolean battleInit = false; // Have battles been started?
	private boolean overworld = true; // is on overworld?
	// TODO: Add stats
	private int hp, str, inTune, lvl, monay, foresight, nimble;
	private int xp;
	private int[] allStats = {hp, str,inTune, lvl, monay, foresight,nimble,xp};
	// TODO: Add inventory 
	//private Inventory inventory;
	

	
	public Creature(String sprite) {
		player_sprite = new Sprite(sprite);
		player_sprite.setX(15);
		player_sprite.setY(15);
		this.x = player_sprite.getX();
		this.y = player_sprite.getY();
		this.alive = true;
		
		initStats();
	}
	
	public void initStats() {
		hp = 20;
		str = 16;
		inTune = 14;
		lvl = 1;
		monay = 100;
		foresight = 13;
		nimble = 16;
		xp = 0;
		

	}
	
	public void move() {
		
		// If on overworld move normally.
		if (overworld) {
			player_sprite.setX(player_sprite.getX() + dx);
			player_sprite.setY(player_sprite.getY() + dy);

			// If the new tile is not passable return the player to it's original position.
			if (!level.findSprite(player_sprite.getX(), player_sprite.getY()).isPassable()){
				player_sprite.setX(player_sprite.getX() - dx);
				player_sprite.setY(player_sprite.getY() - dy);
			}

			if (inDangerZone && (dx != 0 || dy != 0)) {
				if ((int)(Math.random() * 20) >= 19) {
					battleInit = true;
					overworld = false;
				}

			}

			dx = 0;
			dy = 0;

			if (x > 19)
				x = 19;
			if (x < 0)
				x = 0;
			if (y < 0)
				y = 0;
			if (y > 19)
				y = 19;

		}
		
		// If in battle, redirect input actions.
		// Works for now
		if (battleInit) {
			
			cursorDir = 0;
			if (dy < 0) {
				cursorDir = -1;
				
			}
			
			if (dy > 0) {
				cursorDir = 1;
				
			}
			
		}
		
		
	}
	
	
	
	
	
	
	
	public void KeyPressed(KeyEvent e) {
		int k = e.getKeyCode();
		
		if (k == KeyEvent.VK_UP)
			dy = -1;
		if (k == KeyEvent.VK_DOWN)
			dy = 1;
		if (k == KeyEvent.VK_RIGHT)
			dx = 1;
		if (k == KeyEvent.VK_LEFT)
			dx = -1;
		if (k == KeyEvent.VK_ENTER)
			selectPressed = true;
		
		if (k == KeyEvent.VK_I)
			inventoryMenu = !inventoryMenu;
		if (k == KeyEvent.VK_ESCAPE) {
			battleInit = false;
			overworld = true;
		}
	}
	
	public void KeyReleased(KeyEvent e) {
		int k = e.getKeyCode();
		
		if (k == KeyEvent.VK_UP) {
			dy = 0;
		
		}
		if (k == KeyEvent.VK_DOWN)
			dy = 0;
		if (k == KeyEvent.VK_RIGHT)
			dx = 0;
		if (k == KeyEvent.VK_LEFT)
			dx = 0;
		if (k == KeyEvent.VK_ENTER)
			selectPressed = false;
		
	}
	
	public Sprite getSprite() {
		return player_sprite;
		
	}
	
	public int getX() {
		//return x;
		return player_sprite.getX();
		
	}
	
	public int getY() {
		//return y;
		return player_sprite.getY();
		
	}
	
	public int[] getAllStats() {
		return allStats;
	}
	
	public int getSingleStat(int stat) {
		return allStats[stat];
	}
	
	public boolean isAlive() {
		return alive;
		
	}

	public boolean InventoryOpen() {
		return inventoryMenu;
	}
	
	public void setLevel(LevelLoader level) {
		this.level = level;
	}
	
	public void toggleSelect() {
		selectPressed = !selectPressed;
	}
	
	public boolean pressedSelect() {
		return selectPressed;
	}
	
	public boolean isOnDoor() {
		boolean door = level.findSprite(player_sprite.getX(), player_sprite.getY()).isDoor();
		return door;
	}
	
	public void isInDangerZone(boolean danger) {
		inDangerZone = danger;
	}
	
	public boolean isInBattle() {
		return battleInit;
	}
	
	public boolean startBattle() {
		return battleInit;
	}
	
	public boolean isOnOverworld() {
		return overworld;
	}
	
	public void setisOnOverworld(boolean overworld) {
		this.overworld = overworld;
	}
	
	// For menu actions in battle. If > 0, move up menu, else move down
	// menu.
	public int getCursorDir() {
		return cursorDir;
		
	}
	

	public void setStats(int [] stats) {
		allStats = stats;
	}
	
	public void setStats(int stat, int val) {
		allStats[stat] = val;
	}
	
	
	
	
	/* TODO: Add Inventory
	public Inventory getInventory() {
		return inventory;
		
	}
	*/
}

